package fr.umlv.m2.uwp2011.persistence.sujet.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import fr.umlv.m2.uwp2011.persistence.sujet.INoteDAO;
import fr.umlv.m2.uwp2011.persistence.sujet.Note;
import fr.umlv.m2.uwp2011.persistence.sujet.Sujet;

public class HibernateNoteDAO implements INoteDAO {
	private static final Map<Long, Note> allNote = new HashMap<Long, Note>();
	private static final  HibernateSujetDAO sujetDAO = new HibernateSujetDAO();

	static {
		Note note;
		Sujet sujet;
		//----------- sujet id 5 ----------------------------//
		sujet = sujetDAO.findById(5L);

		//----------- note id 1  ---------//
		note = new Note();
		note.setId(1L);
		note.setLibelle("14,4");
		note.setSujet(sujet);
		note.setUser_id("etud1");
		
		//----------- note id 2  ---------//
		note = new Note();
		note.setId(2L);
		note.setLibelle("12");
		note.setSujet(sujet);
		note.setUser_id("etud2");
		
		//----------- note id 3  ---------//
		note = new Note();
		note.setId(3L);
		note.setLibelle("10");
		note.setSujet(sujet);
		note.setUser_id("etud3");
		
		//----------- note id 4  ---------//
		note = new Note();
		note.setId(4L);
		note.setLibelle("8,5");
		note.setSujet(sujet);
		note.setUser_id("etud4");
	}

	@Override
	public List<Note> findByUserID(String user_id) {
		List<Note> list = new ArrayList<Note>();
		for(Note note: allNote.values()) {
			if(note.getUser_id() == user_id) {
				list.add(note);
			}
		}
		return list;
	}

	@Override
	public List<Note> findBySujet(Long sujet_id) {
		List<Note> list = new ArrayList<Note>();
		for(Note note: allNote.values()) {
			if(note.getSujet().getId() == sujet_id) {
				list.add(note);
			}
		}
		return list;
	}

	@Override
	public Note findByUserAndSujet(String user_id, Long sujet_id) {	
		for(Note note: allNote.values()) {
			if(note.getUser_id() == user_id && note.getSujet().getId() == sujet_id) {
				return note;
			}
		}
		return null;
	}
	
	
}
